<template>
  <div class="employee-container">
    <div class="container">
      <div class="tableBar">
        <el-input
          v-model="searchText"
          placeholder="请输入员工姓名"
          prefix-icon="el-icon-search"
          clearable
          style="width: 300px; margin-bottom: 20px"
          @keyup.enter.native="init"
        />
        <el-button
          type="primary"
          style="float: right"
          @click="$router.push('/add')"
        >+ 添加员工</el-button>
        <el-table :data="employees" style="width: 100%; margin-top: 20px">
          <el-table-column prop="name" label="员工姓名" />
          <el-table-column prop="username" label="账号" />
          <el-table-column prop="phone" label="手机号" />
          <el-table-column label="账号状态">
            <template slot-scope="scope">
              {{ String(scope.row.status) === "1" ? "已禁用" : "正常" }}
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center">
            <template v-slot="{ row }">
              <el-link
                type="primary"
                @click="$router.push('/add?id=' + row.id)"
              >编辑</el-link>
              <el-divider direction="vertical" />
              <el-popconfirm
                title="确认调整该账号的状态?"
                @onConfirm="handleChangeStauts(row)"
              >
                <el-link slot="reference" type="danger">{{
                  row.status == "0" ? "禁用" : "启用"
                }}</el-link>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <div class="footer">
          <el-pagination
            :current-page="pageInfo.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getEmployees, changeStauts } from '@/api/employee'
export default {
  name: 'Employee',
  data() {
    return {
      searchText: '',
      employees: [],
      counts: 0,
      pageInfo: {
        page: 1,
        pageSize: 10
      },
      status: ''
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      getEmployees({ ...this.pageInfo, name: this.searchText }).then((res) => {
        const { records, total } = res.data
        this.employees = records
        this.counts = total
      })
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1
      this.pageInfo.pageSize = val
      this.init()
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val
      this.init()
    },

    async handleChangeStauts(row) {
      await changeStauts({ id: row.id, status: +!row.status })
      this.$message.success('改变成功')
      this.init()
    }
  }
}
</script>

  <style lang="scss" scoped>
.employee-container {
  .container {
    margin: 30px;
    .footer {
      margin-top: 33px;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
